using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using DynSim.EntityFramework;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Diagnostics;

namespace DynSim.Basics
{
    [Serializable]
    public class Scene
    {
        public EntityCollection entities = new EntityCollection();
    }

    public class SceneReader
    {
        const int VERSION = 1;
        public static void Save(Scene scene, string fileName)
        {
            Stream stream = null;
            try
            {
                IFormatter formatter = new BinaryFormatter();
                stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, VERSION);
                formatter.Serialize(stream, scene);
            }
            catch
            {

            }
            finally
            {
                if (null != stream)
                    stream.Close();
            }
        }

        public static Scene Load(string fileName)
        {
            Stream stream = null;
            Scene settings = null;
            try
            {
                IFormatter formatter = new BinaryFormatter();
                stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
                int version = (int)formatter.Deserialize(stream);
                Debug.Assert(version == VERSION);
                settings = (Scene)formatter.Deserialize(stream);
            }
            catch
            {

            }
            finally
            {
                if (null != stream)
                    stream.Close();
            }
            return settings;
        }
    }
}
